using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;

namespace converter.template.item
{
    internal class TableCell : TemplateItem
    {
        public bool IsBodyCell
        {
            get
            {
                return isBodyCell;
            }
            set
            {
                isBodyCell = value;
            }
        }

        /// <summary>
        /// Creates the TableCell element.
        /// </summary>
        /// <param name="parent">The parent element -- TableRow.</param>
        public override void Build(OpenXmlCompositeElement parent)
        {
            DocumentFormat.OpenXml.Wordprocessing.TableCell cell = new DocumentFormat.OpenXml.Wordprocessing.TableCell();

            TableCellProperties cellProperties = new TableCellProperties();

            // Cell borders
            TableCellBorders cellBorders = new TableCellBorders();
            TopBorder topBorder = new TopBorder() { Val = GetStyle().Borders.TopBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            LeftBorder leftBorder = new LeftBorder() { Val = GetStyle().Borders.LeftBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            BottomBorder bottomBorder = new BottomBorder() { Val = GetStyle().Borders.BottomBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            RightBorder rightBorder = new RightBorder() { Val = GetStyle().Borders.RightBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            cellBorders.Append(topBorder);
            cellBorders.Append(leftBorder);
            cellBorders.Append(bottomBorder);
            cellBorders.Append(rightBorder);

            cellProperties.Append(cellBorders);

            // Columns span property.
            if (colSpan > 1)
            {
                GridSpan gridSpan = new GridSpan() { Val = colSpan, };
                cellProperties.Append(gridSpan);
            }

            cell.Append(cellProperties);

            // Build the cell's content.
            // If the cell does not contain any content put in an empty paragraph.
            if (Items.Count == 0)
            {
                cell.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
            }
            else
            {
                foreach (TemplateItem item in Items)
                    item.Build(cell);

                // If the last element is a table, add a new empty paragraph, otherwise the document won't open properly.
                if (Items.Last() is Table)
                    cell.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
            }

            parent.Append(cell);
        }

        public int GetColSpan()
        {
            return colSpan;
        }

        public void SetColSpan(int value)
        {
            colSpan = value;
        }

        private int colSpan = 1;
        bool isBodyCell = true;
    }
}
